package cn.tedu.api;

import java.util.Objects;

/*本类用于回顾Object常用方法的使用*/
public class TestObject2 {
    public static void main(String[] args) {
        //4.1使用无参构造,创建小猫类的对象c1 c2
        Cat c1 = new Cat();
        Cat c2 = new Cat();
        //4.2使用全参构造,创建小猫类的对象c3 c4 ("机器猫",300,10000.99,"大雄",'男')
        Cat c3 = new Cat("机器猫",300,10000.99,"大雄",'男');
        Cat c4 = new Cat("机器猫",300,10000.99,"大雄",'男');

        //8.1使用无参构造创建小狗类的对象d1 d2
        Dog d1 = new Dog();
        Dog d2 = new Dog();
        //8.2使用全参构造创建小狗类的对象d3 d4 ("旺财",5,500)
        Dog d3 = new Dog("旺财",5,500);
        Dog d4 = new Dog("旺财",5,500);

        //9.1打印c1对象的地址值
        System.out.println(c1);
        //9.2打印d2对象的类型+属性+属性值
        System.out.println(d2);
        //9.3以下两个对象打印出来的内容是什么?
        //Dog类重写了toString()  Cat类没有重写,使用的是父类Object的默认实现
        System.out.println(d3);//Dog{name='旺财', age=5, price=500.0}
        System.out.println(c3);//cn.tedu.api.Cat@4554617c

        //10.1 使用==的底层逻辑比较c3 c4 两个对象
        System.out.println(c3 == c4);//false
        System.out.println(c3.equals(c4));//false
        //10.2 比较d3 d4 两个对象的类型 属性 属性值
        //重写后equals()不用==比较,不比较地址值,比较类型 属性 属性值
        System.out.println(d3 == d4);//false,==永远比较的是引用类型保存的地址值
        System.out.println(d3.equals(d4));//true
    }
}
//1.创建小猫类Cat
class Cat{
    //2.定义小猫类的属性 姓名name 年龄age 价格price 主人host 性别gender
    String name;
    int age;
    double price;
    String host;
    char gender;
    //3.创建小猫类的无参构造与全参构造
    public Cat() {}
    public Cat(String name, int age, double price, String host, char gender) {
        this.name = name;
        this.age = age;
        this.price = price;
        this.host = host;
        this.gender = gender;
    }

    /*@Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Cat cat = (Cat) o;
        return age == cat.age && Double.compare(cat.price, price) == 0 && gender == cat.gender && Objects.equals(name, cat.name) && Objects.equals(host, cat.host);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, price, host, gender);
    }*/
}
//5.创建小狗类Dog
class Dog{
    //6.定义小狗类的属性 name age price
    String name;
    int age;
    double price;
    //7.提供小狗类的无参与全参构造
    public Dog() {}
    public Dog(String name, int age, double price) {
        this.name = name;
        this.age = age;
        this.price = price;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", price=" + price +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Dog dog = (Dog) o;
        return age == dog.age && Double.compare(dog.price, price) == 0 && Objects.equals(name, dog.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, price);
    }
}










